import TreeTraversal.TreeNode;

import java.util.LinkedList;

public class isValidBST {
    public boolean isValidBST(TreeNode root) {
        TreeNode p = root;
        long prev = Long.MIN_VALUE;
        LinkedList<TreeNode> stack = new LinkedList<>();
        while( p != null || !stack.isEmpty()){
            if(p != null){
                stack.push(p);
                p = p.left;
            }else{
                TreeNode pop = stack.pop();
                if(prev >= pop.val){
                    return false;
                }
                prev = pop.val;
                p = pop.right;
            }
        }
        return true;
    }
}
